package day06;

import java.util.Arrays;

/**
 * 待解决：迭代的方式处理有问题
 * 归并排序的递归和非递归实现
 */
public class Code01_MergeSort {

    /**
     * 递归方法实现
     */
    public static void mergeSort1(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process(arr, 0, arr.length);
    }

    // 请把arr[L..R)排有序
    // T(N) = 2 * T(N / 2) + O(N)
    // O(N * logN)
    public static void process(int[] arr, int begin, int end) {
        //递归出口
        if (end - begin < 2) {
            return;
        }
        int mid = (end + begin) >> 1;
        process(arr, begin, mid);
        process(arr, mid, end);
        merge(arr, begin, mid, end);
    }

    /**
     * 将 [begin, mid) 和 [mid, end) 范围的序列合并成一个有序序列
     * 将 两个个数相差不大的有序的序列 合成  一个有序的序列
     */
    public static void merge(int[] arr, int begin, int mid, int end) {
        int li = 0, length = (end - begin) >> 1;
        int ri = mid, re = end;
        int ai = begin;
        int[] leftArray = new int[(end - begin) >> 1];
        // 备份左边数组
        for (int i = 0; i < length; i++) {
            leftArray[i] = arr[begin + i];
        }

        while (li < length && ri < re) {
            if (arr[ri] < leftArray[li]) {
                arr[ai++] = arr[ri++];
            } else {
                arr[ai++] = leftArray[li++];
            }
        }
        while (li < length) {
            arr[ai++] = leftArray[li++];
        }
        while (ri < re) {
            arr[ai++] = arr[ri++];
        }
    }

    /**
     * 非递归方法实现
     * <p>
     * 设置的步长有问题。 需要去重新排查一下，具体该怎么设置此值
     */
    public static void mergeSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int length = arr.length;
        // 步长
        int mergeSize = 1;
        while (mergeSize < length) {
            printArray(arr);
            // 当前左组的，第一个位置
            int L = 0;
            int R = length;
            while (L < length) {
                if (mergeSize > ((length >> 1) + 1)) {
                    break;
                }
                System.out.println("mergeSize=" + mergeSize);

                int mid = L + mergeSize;
                R = mid + Math.min(mergeSize, length - mid);
                merge(arr, L, mid, R);
                L = R;
            }
            mergeSize = mergeSize << 1;

        }
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 5;
        int maxValue = 5;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            arr1 = new int[]{2, -1, 1, -1, -2};
            int[] arr2 = copyArray(arr1);
            //mergeSort1(arr1);
            Arrays.sort(arr1);
            mergeSort1(arr2);
            if (!isEqual(arr1, arr2)) {
                System.out.println("出错了！");
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println("测试结束");
    }

}
